library(plyr)
library(estimatr)
library(emmeans)
library(tidyverse)
library(magrittr)
library(broom)
library(modelsummary)
library(kableExtra)

d1 <- "https://github.com/thomasjwood/full_fact/raw/main/ff_mt.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS %>% 
  mutate(
    out_agree = out_agree %>% 
      mapvalues(
        c("Tend to disagree",
          "Tend to agree"),
        c("Disagree",
          "Agree")
      ) %>% 
      factor(
        c("Strongly disagree",
          "Disagree",
          "Neither agree nor disagree",
          "Agree",
          "Strongly agree")
      ),
    out_true = out_true %>% 
      factor(
        c("True",
          "Probably true",
          "Not sure",
          "Probably false",
          "False") %>% 
          rev
      ),
    agree_num = out_agree %>% 
      as.numeric,
    true_num = out_true %>% 
      as.numeric,
    cond = cond %>% 
      factor(
        c("cond_items",
          "cond_misinfo",
          "cond_corr")
      )
  )

d1$com_num <- d1$agree_num %>% 
  add(
    d1$true_num
  ) %>% 
  divide_by(2)

t1 <- d1 %>% 
  mutate(
    country = issue %>% 
      str_detect(
        "global|saltwater"
      ) %>% 
      ifelse(
        "Multi-country",
        country
      )
  ) %>%
  filter(
    wave == "wave 1"
  ) %>% 
  group_by(
    country, wave, issue
  ) %>% 
  nest 


t1$mods <- t1$data %>% 
  map(
    ~lm_robust(
      com_num ~ cond, 
      data = .x 
    )
  )

t1$emm <- t1$mods %>% 
  map2(
    t1$data,
    ~emmeans(
      .x, 
      consec ~ cond, 
      data = .y
    )
  )

t1$edf <- t1$emm %>% 
  map(
    ~extract2(., 1) %>% 
      tidy
  )

t1$cont <- t1$emm %>% 
  map(
    ~extract2(., 2) %>% 
      tidy
  )


l_m <- t1 %>% 
  ungroup %>% 
  select(
    country, issue, mods 
  ) %>%
  mutate(
    country = t1$country %>% 
      str_to_title %>% 
      factor(
        c("Multi-Country",
          "Nigeria",
          "South Africa",
          "Argentina",
          "United Kingdom")
      ),
    issue = issue %>% 
      factor(
        t1 %>% 
          ungroup %>% 
          select(issue, edf) %>% 
          pmap_dfr(
            function(issue, edf)
              edf %>% 
              mutate(issue = issue)
          ) %>% 
          group_by(issue) %>% 
          summarize(mu = estimate %>% mean) %>% 
          arrange(mu) %>% 
          use_series(issue)
      )
  ) %>% 
  arrange(country, issue) %>% 
  filter(
    country %>% 
      is_in(
        c("Multi-Country",
          "Nigeria",
          "South Africa")
      )
  )

l_m$mods %<>% 
  set_attr(
    "names", 
    l_m$issue %>% 
      str_sub(
        l_m$issue %>% 
          str_locate("_") %>% 
          extract(, 2) %>% 
          add(1)
      ) %>% 
      str_to_title %>% 
      str_replace_all(
        c("Birthreg" = "BirthReg",
          "Youthue" = "YouthUE",
          "Crimestats" = "CrimeStats",
          "Resbank" = "ResBank",
          "Civserv" = "CivServ")
      )
  )

l_m$mods %>% 
  modelsummary(
    coef_rename = c(
      "condcond_misinfo"= "Cond(Misinfo)",
      "condcond_corr"   = "Cond(Correction)"
    ),
    stars =  T,
    # output = "latex", 
    gof_omit =  "R2 Adj.|se_type",
    coef_omit = "(Intercept)",  
    fmt = "%.2f"
  ) %>% 
  add_header_above(
    c(" " =  1,
      "Multi-Country" = 2,
      "Nigeria" = 5,
      "South Africa" = 5)
  )


l_m2 <- t1 %>% 
  ungroup %>% 
  select(
    country, issue, mods 
  ) %>%
  mutate(
    country = t1$country %>% 
      str_to_title %>% 
      factor(
        c("Multi-Country",
          "Nigeria",
          "South Africa",
          "Argentina",
          "United Kingdom")
      ),
    issue = issue %>% 
      factor(
        t1 %>% 
          ungroup %>% 
          select(issue, edf) %>% 
          pmap_dfr(
            function(issue, edf)
              edf %>% 
              mutate(issue = issue)
          ) %>% 
          group_by(issue) %>% 
          summarize(mu = estimate %>% mean) %>% 
          arrange(mu) %>% 
          use_series(issue)
      )
  ) %>% 
  arrange(country, issue) %>% 
  filter(
    country %>% 
      is_in(
        c("Argentina",
          "United Kingdom")
      )
  )

l_m2$mods %<>% 
  set_attr(
    "names", 
    l_m2$issue %>% 
      str_sub(
        l_m2$issue %>% 
          str_locate("_") %>% 
          extract(, 2) %>% 
          add(1)
      ) %>% 
      str_to_title %>% 
      str_replace_all(
        c("Birthreg" = "BirthReg",
          "Youthue" = "YouthUE",
          "Crimestats" = "CrimeStats",
          "Resbank" = "ResBank",
          "Civserv" = "CivServ")
      )
  )


l_m2$mods %>% 
  modelsummary(
    coef_rename = c(
      "condcond_misinfo"= "Cond(Misinfo)",
      "condcond_corr"   = "Cond(Correction)"
    ),
    stars =  T,
    gof_omit =  "R2 Adj.|se_type",
    coef_omit = "(Intercept)",  
    fmt = "%.2f"
  ) %>% 
  add_header_above(
    c(" " =  1,
      "Argentina" = 5,
      "United Kingdom" = 5)
  )


